package com.mytoutou.playermanager.slice;

import com.mytoutou.playermanager.player_manager.ui.MediaPlayerWrapper;
import com.mytoutou.playermanager.player_manager.ui.VideoPlayerView;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.InnerEvent;

import java.util.HashMap;
import java.util.Map;

public class MyMainThreadMediaPlayerListener implements MediaPlayerWrapper.MainThreadMediaPlayerListener {
    private EventHandler handler;
    private VideoPlayerView view;
    /**
     * 下载MP4进度
     */
    public static final int PRESS_VALUE = 0x1005;

    public MyMainThreadMediaPlayerListener(EventHandler handler, VideoPlayerView view) {
        this.handler = handler;
        this.view = view;
    }

    @Override
    public void onVideoSizeChangedMainThread(int width, int height) {
        view.setOnVideoStateChangedListener(new MediaPlayerWrapper.VideoStateListener() {
            @Override
            public void onVideoPlayTimeChanged(int positionInMilliseconds) {
                System.out.println("播放时间改变：" + positionInMilliseconds);
                long duration = view.getDuration();
                float percent = ((float) positionInMilliseconds / (float) duration) * 100;
                System.out.println("百分比：" + percent);
                InnerEvent innerEvent = InnerEvent.get(PRESS_VALUE, Double.valueOf(percent).intValue() + "");
                handler.sendEvent(innerEvent);
            }
        });

    }

    @Override
    public void onVideoPreparedMainThread() {

    }

    @Override
    public void onVideoCompletionMainThread() {
        //重播
        view.replay(true);
    }

    @Override
    public void onErrorMainThread(int what, int extra) {

    }

    @Override
    public void onBufferingUpdateMainThread(int percent) {

    }

    @Override
    public void onVideoStoppedMainThread() {
    }
}
